#include "udp_sample.h"

int main() {
	struct sockaddr_in sock_server;
	struct sockaddr_in sock_client;
	int s;
	socklen_t socklen = sizeof(struct sockaddr_in);
	char buf[BUFLEN];

	if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
		diep("socket()");
	}
	sock_server.sin_family = AF_INET;
	sock_server.sin_port = htons(PORT);
	sock_server.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(s, (struct sockaddr *) &sock_server, socklen) == -1) {
		diep("bind()");
	}

	for (;;) {
		if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &sock_client, &socklen) == -1)
			diep("recvfrom()");

		printf("Received packet from %s:%d\nData: %s\n", 
			   inet_ntoa(sock_client.sin_addr),
			   ntohs(sock_client.sin_port), buf);

	}

	close(s);
	return 0;
}
